import psutil
import json
from app.machines.mac import MacInfo

class DiskInfo:

    def get_disk_info():
        disk_dict = {}
        # 磁盘分区信息
        partitions = psutil.disk_partitions()
        sdiskparts = []
        for p in partitions:
            print("@@" + p.device)
            sdiskparts.append(p.device)
        print(partitions)
        # 磁盘使用情况
        usages = psutil.disk_usage('/')
        # 磁盘总量大小 单位:G
        disk_total_size = round(usages.total / 1024 / 1024 / 1024, 2)
        # 磁盘已用量 单位:G
        disk_used = round(usages.used / 1024 / 1024 / 1024, 2)
        # 磁盘剩余量 单位:G
        disk_free = round(usages.free / 1024 / 1024 / 1024, 2)
        # 磁盘使用率
        disk_use_percent = usages.percent

        print(usages)
        # 磁盘IO
        ios = psutil.disk_io_counters()
        # 总共读的次数
        io_read_count = ios.read_count
        # 总共写的次数
        io_write_count = ios.write_count
        # 总读的数据大小 单位:M
        io_read_size = round(ios.read_bytes / 1024 / 1024 , 2)
        # 总写的数据大小 单位:M
        io_write_size = round(ios.write_bytes / 1024 / 1024 , 2)
        # 总读的耗时 单位:秒
        io_read_time = ios.read_time
        # 总写的耗时
        io_write_time = ios.write_time

        disk_dict['mac_code'] = MacInfo.get_mac()
        disk_dict['disk_parts'] = ''.join(sdiskparts)
        disk_dict['disk_total_size'] = disk_total_size
        disk_dict['disk_used'] = disk_used
        disk_dict['disk_free'] = disk_free
        disk_dict['disk_use_percent'] = disk_use_percent
        #--------------------------------------------------
        disk_dict['io_read_count'] = io_read_count
        disk_dict['io_write_count'] = io_write_count
        disk_dict['io_read_size'] = io_read_size
        disk_dict['io_write_size'] = io_write_size
        disk_dict['io_read_time'] = io_read_time
        disk_dict['io_write_time'] = io_write_time

        print(ios)
        return disk_dict

if __name__ == '__main__':
    disk = DiskInfo()
    d = disk.get_disk_info()
    disk_json = json.dumps(d)
    print(disk_json)